
!-------------------------------------------------------------------------------------

subroutine set_ic(dfu, Fbg, it)    ! outputs

  use dimensions
  use mpi_wrap
  use fft_wrap 

  use parameters, only : restart, runname, ModAmp, iLinVlasov, kz

  implicit none


  real,    intent(out) :: dfu(jv, jz, jy, jx, jDir)
  real,    intent(in ) :: Fbg(jv)
  integer, intent(out) :: it

  integer              :: ix, iy, iz, iv, iDir, filenum
  integer              :: jvc

  character*7          :: file_ext
  complex              :: fv(jv), fz(jz)

!-- read restart or create new --

  if (restart) then

       write(msg,*) char(10), ' Reading restart', char(10)
       call mpi_wrap_msg

       open(93, file= trim(runname)//'.tag')
       read(93, *) it, filenum 
       close(93)

       write(*,*) it, filenum

       file_ext = '0000000'
       write(file_ext,"(i4.4'.'i2.2)") filenum, myid

       open(91, file= trim(runname)//'.dfu.'//file_ext, &
            form='unformatted', access='stream')
       read(91) dfu
       close(91)

       return

  endif


 
  write(msg,*) char(10), ' Creating initial data', char(10)
  call mpi_wrap_msg

  it = 0
  dfu=0.0

  !call random_number(dfu)
  !dfu(:,:,:,:,:) = dfu(:,:,:,:,:)*2 - 1
  
  iDir=1
  ix=1
  iy=1
  
  !-- filteing in z
  !
  !do iv=1,jv
  !   fz=dfu(iv,:,iy,ix,idir)
  !   call fft_forward(jz,fz,fz)
  !   fz = fz/float(jz)
  !   fz(1)=0
  !   fz(3:jz-1)=0                 ! single kz-mode
  !   call fft_backward(jz,fz,fz)
  !   dfu(iv,:,iy,ix,idir)=fz;
  !enddo


  !-- filteing in v
  !
  !jvc=jv/4        
  !do iz=1,jz
  !   fv=dfu(:,iz,iy,ix,idir)
  !   call fft_forward(jv,fv,fv)
  !   fv = fv/float(jv)
  !   fv(2+jvc : jv-jvc)=0         ! keep only "jv < jvc" modes
  !   call fft_backward(jv,fv,fv)
  !   dfu(:,iz,iy,ix,idir)=fv;
  !enddo


  !-- normalizing to ModAmp
  !
  !dfu = dfu * ModAmp / maxval(abs(dfu))
 

  !-- single kz mode

  do iz = 1,jz
     dfu(:,iz,:,:,:) = ModAmp * cos(kz*zvec(iz))
  end do

  !-- modulated in z

  if (iLinVlasov) then

     do iv = 1,jv
        dfu(iv,:,:,:,:) = dfu(iv,:,:,:,:) * Fbg(iv)
     end do

  else

     do iv = 1,jv
        dfu(iv,:,:,:,:) = (dfu(iv,:,:,:,:) + 1.0) * Fbg(iv)
     end do

  end if


end subroutine set_ic

!-------------------------------------------------------------------------------------
subroutine write_bin(dfu, Ez2D, it, filenum)
!-------------------------------------------------------------------------------------

  use dimensions
  use parameters, only  :  runname
  use mpi_wrap

  implicit none

  real,    intent(in)    :: dfu(jv, jz, jy, jx, jDir)
  real,    intent(in)    :: Ez2D(jz,jy,jx)
  integer, intent(in)    :: it
  integer, intent(in)    :: filenum


  integer     :: flag
  character*7 :: file_ext

  file_ext = '0000000'
  write(file_ext,"(i4.4'.'i2.2)") filenum, myid


  open(91, file= trim(runname)//'.dfu.'//file_ext, &
         form='unformatted', access='stream')
  write(91) dfu
  close(91)



  if (myid .eq. 0) then

     open(92, file= trim(runname)//'.Ez.'//file_ext, &
          form='unformatted', access='stream')
     write(92) Ez2D
     close(92)


     open(93, file= trim(runname)//'.tag')
     write(93,*) it, filenum 
     close(93)

  end if

 end subroutine write_bin


!-------------------------------------------------------------------------------------
subroutine diagnostics( dfu, Ez2D, t)            ! inputs
!-------------------------------------------------------------------------------------

  use dimensions
  use mpi_wrap


  use parameters, only : runname, restart, ux0

  implicit none

  !-- global variables --

  real, intent(in)       :: dfu(jv, jz, jy, jx, jDir)
  real, intent(in)       :: Ez2D(jz,jy,jx)
  real, intent(in)       :: t


  !-- local variables --

  real       :: KE, PotEx, potEz
  real       :: funiform(jv), funiform1(jv)

  integer    :: ix, iy, iz, iv, iDir

  logical, save          :: first_call = .true.
  integer, save          :: filenum = 0

  character*4            :: file_ext


  !-----------------------------------------------------------------------------------
       
       
      do iv=1,jv
         funiform1(iv) = sum( dfu(iv,:,:,:,:) )
      end do


      call mpi_reduce(funiform1, funiform, jv, &
           MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, mpi_err)


      if (myid .ne. 0) return
     
      funiform = funiform /(jx*jy*jz)

      PotEz = 0.5*sum(Ez2D(:,:,:)**2) / (jx*jy*jz)

      KE =  0.5 * sum( funiform(:) * vvec(:)**2 ) * dv


      !------------------------------
       
      open(3, file=trim(runname)//'.dat', position='append')

      if (first_call .and. .not. restart) then
  
         write(3, "('% 1.t  2.KineticEnergy  3.Ez^2energy')")
         write(3,*)

         write(*, "('% 1.t  2.KineticEnergy  3.Ez^2energy')")
         write(*,*)

      end if

      write(3,501) t, KE, potEz
      write(*,501) t, KE, potEz

      close(3)


      !------------------------------

      first_call = .false.

    501   format(  6400(e12.6,2x)  )


end subroutine diagnostics


!-------------------------------------------------------------------------------------
